{-# LANGUAGE DeriveDataTypeable #-}
----------------------------------------------------------------
--                                                            --
-- Module      :  LLVM.Code.Alias                             --
-- License     :  GPL3                                        --
-- Mantainer   :  Mikhail Belyaev                             --
--                                                            --
----------------------------------------------------------------

module LLVM.Code.Alias where

import Data.Generics(Typeable, Data)

import LLVM.Parser.Record.Record
       (recordAsList, RawBlock(..), RecordType(..))
import LLVM.Code.Shared
       (LinkageType(..), VisibilityType(..), LinkageType,
        VisibilityType, ValIndex(..), TypeIndex(..))

import LLVM.Util
import LLVM.Code.Types (Type(..), getTypeByIndex)
import LLVM.Code.Values.Kinds (AliasValue(..))
import LLVM.Code.Values (Value(..), TypedValue(..))


data AliasDef = AliasDef TypeIndex AliasValue deriving (Show, Eq, Typeable, Data)

instance RecordType AliasDef where
    fromRawRecord rr = case (recordAsList rr) of
                            (_:9:t:v:l:vis:_) -> Just $ AliasDef rt $ AliasValue rv rvis rl
                                                where rt = fromInteger t
                                                      rv = fromInteger v
                                                      rvis = integerToIC vis ?? DefaultVis
                                                      rl = integerToIC l ?? External
                            _ -> Nothing


instance TypedValue AliasDef where
    getType (AliasDef ti _) tt = getTypeByIndex tt ti ?? error "Alias type not found"
    toValue (AliasDef _ av) = AliasV av

